<template>
  <div
    class="cover-fence"
    :class="customClass"
    :style="gridStyle"
  >
    <div
      class="cover-fence-cell"
      v-for="(item,index) in items"
      :key="index"
      :style="columStyle"
    >
      <van-cell
        :title="item.title"
        :label="item.description"
        center
        :border="false"
        :style="backgroundStyle"
        value-class="cover-fence-cell-value"
        :url="item.link"
      >
        <image
          v-if="item.cover"
          :src="item.cover"
          mode="aspectFill"
        />
      </van-cell>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CoverFence',
  props: {
    items: {
      type: Array,
      default: []
    },
    customClass: String,
    columnNum: {
      type: Number,
      default: 2
    },
    gutter: [Number, String],
    background: String,
    iconWidth: [Number, String],
    iconHeight: [Number, String]
  },
  computed: {
    gridStyle () {
      if (this.gutter) {
        let padding = this.addUnit(this.gutter)
        return `padding: ${padding};`
      }
      return false
    },
    columStyle () {
      let style = []
      if (this.columnNum) {
        let width = 100 / this.columnNum
        style.push(`width:${width}%`)
      }
      if (this.gutter) {
        let padding = this.addUnit(this.gutter)
        style.push(`padding:${padding}`)
      }
      return style.join(';')
    },
    backgroundStyle () {
      return this.background ? `background: ${this.background};` : false
    },
    iconStyle () {
      let style = []
      if (this.iconWidth) {
        let width = this.addUnit(this.iconWidth)
        style.push(`width:${width}`)
      }
      if (this.iconHeight) {
        let height = this.addUnit(this.iconHeight)
        style.push(`height:${height}`)
      }
      return style.join(';')
    }
  },
  methods: {
    addUnit (value) {
      if (value) {
        return /^\d+(\.\d+)?$/.test(value) ? value + 'px' : value
      }
      return undefined
    }
  }
}
</script>

<style lang="less" scoped>
.cover-fence {
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  flex-wrap: wrap;
  &-cell {
    flex-basis: 50%;
    position: relative;
    overflow: hidden;
    /deep/ .van-cell {
      padding: 10px;
      &__title {
        z-index: 1;
        color: #fff;
      }
      &__label {
        color: rgba(255, 255, 255, 0.8);
      }
      &__value {
        flex: none;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
        z-index: 0;
        border-radius: 5px;
        &::before {
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
          overflow: hidden;
          z-index: 0;
          content: "";
          background-color: rgba(0, 0, 0, 0.3);
          background-repeat: no-repeat;
          background-position: center center;
          background-size: cover;
        }
      }
      image {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
